package com.metaera.app.ui.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LikeInformationPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.activities.ArticleDetailsActivity;
import com.metaera.app.ui.activities.UserProfileActivity;
import com.metaera.app.ui.popup.SharePopup;
import com.metaera.app.utils.DateTime;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.image.GlideApp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;

public class HomeFollowNewsAdapter extends BaseQuickAdapter<ArticleListResponse, BaseViewHolder> {

    public HomeFollowNewsAdapter() {
        super(R.layout.item_home_follow_news);

        addChildClickViewIds(R.id.tv_share, R.id.tv_like_number, R.id.iv_avatar);

        setOnItemChildClickListener((adapter, v, position) -> {
            Context context = v.getContext();

            if (v.getId() == R.id.tv_share) {
                ArticleListResponse data = getItem(position);

                String desc = data.getContent();
                if (getChinese(desc).length() > 20) {
                    desc = getChinese(desc).substring(0, 20);
                }

                new XPopup.Builder(v.getContext())
                        .asCustom(
                                new SharePopup(
                                        v.getContext(),
                                        data.getTitle(),
                                        desc + "...",
                                        ApiParam.LINK_SHARE_5 + data.getId(),
                                        R.mipmap.ic_launcher
                                )
                        )
                        .show();

            } else if (v.getId() == R.id.tv_like_number) {
                ArticleListResponse item = getItem(position);
                LikeInformationPresenter presenter = new LikeInformationPresenter();
                presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                    // 改变点击数
                    item.setLike_num(item.getLike_num() + (!v.isSelected() ? 1 : -1));
                    // 设置点赞状态
                    item.setIs_like(1);
                    v.setSelected(!v.isSelected());
                    ((TextView) v).setText(String.valueOf(item.getLike_num()));
                }));

                presenter.like(item.getId(), !v.isSelected());

            } else if(v.getId() == R.id.iv_avatar){
                context.startActivity(UserProfileActivity.newIntent(context, getItem(position).getAuthor().getId()));
            }
        });

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(ArticleDetailsActivity.newInstance(context, getItem(position).getId()));
        });

    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ArticleListResponse item) {
        ArticleListResponse.Author author = item.getAuthor();

        View view = holder.getView(R.id.tv_like_number);
        view.setSelected(item.getIs_like() == 1);

        holder.setText(R.id.tv_title, item.getTitle())
                .setText(R.id.tv_date, DateTime.format(item.getRelease_time_stamp()))
                .setText(R.id.tv_like_number, String.valueOf(item.getLike_num()))
                .setText(R.id.tv_name, author.getNickname());

        ImageView ivCover = holder.getView(R.id.iv_cover);
        GlideApp.with(ivCover).load(item.getPic()).normal().into(ivCover);

        ImageView ivAvatar = holder.getView(R.id.iv_avatar);
        GlideApp.with(ivAvatar).load(author.getAvatar_img()).avatar().into(ivAvatar);
    }


    public String getChinese(String paramValue) {
        String regex = "([\u4e00-\u9fa5]+)";
        String str = "";
        Matcher matcher = Pattern.compile(regex).matcher(paramValue);
        while (matcher.find()) {
            str += matcher.group(0);
        }
        return str;
    }
}
